<?xml version="1.0" encoding="ISO-8859-1" ?>
<!-- Copyright 2016-2017 Steinar Bang                                                -->
<!--                                                                                 -->
<!-- Licensed under the Apache License, Version 2.0 (the "License");                 -->
<!-- you may not use this file except in compliance with the License.                -->
<!-- You may obtain a copy of the License at                                         -->
<!--   http://www.apache.org/licenses/LICENSE-2.0                                    -->
<!-- Unless required by applicable law or agreed to in writing,                      -->
<!-- software distributed under the License is distributed on an "AS IS" BASIS,      -->
<!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.        -->
<!-- See the License for the specific language governing permissions and limitations -->
<!-- under the License.                                                              -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Ukel�nn administrasjon</title>
    </h:head>
    <h:body>
        <h1>Hei #{ukelonnAdmin.administratorFornavn}!</h1>

        <f:view>
            <p:accordionPanel id="tabs">
                <p:tab title="Registrere utbetaling">
                    <p:panelGrid columns="1">
                        <h:form id="selectUser">
                            <p:selectOneMenu id="selectedAccount" value="#{ukelonnAdmin.account}" converter="accountConverter">
                                <f:selectItem itemLabel="Velg hvem det skal betales til" itemValue="" noSelectionOption="true" />
                                <f:selectItems value="#{ukelonnAdmin.accounts}" var="account2" itemValue="#{account2}" itemLabel="#{account2.fullName}"/>
                                <p:ajax event="change" update=":tabs:balanse:balanse :tabs:utbetal:paymenttype :tabs:utbetal:amount :tabs:userinfo:jobs:jobs :tabs:userinfo:payments:payments"/>
                            </p:selectOneMenu>
                        </h:form>
                        <h:form id="balanse">
                            <p:panelGrid columns="2">
                                <h:outputLabel for="balanse" value="Til gode: " />
                                <h:inputText id="balanse" value="#{ukelonnAdmin.balanse}" required="true"/>
                            </p:panelGrid>
                        </h:form>
                        <h:form id="utbetal">
                            <p:panelGrid columns="3">
                                <p:selectOneMenu id="paymenttype" value="#{ukelonnAdmin.newPaymentType}" converter="transactionTypeConverter">
                                    <f:selectItem itemLabel="Registrer utbetaling" itemValue="" noSelectionOption="true" />
                                    <f:selectItems value="#{ukelonnAdmin.paymentTypes}" var="payment2" itemValue="#{payment2}" itemLabel="#{payment2.transactionTypeName}"/>
                                    <p:ajax event="change" update="amount" listener="#{ukelonnAdmin.newPaymentTypeSelected}"/>
                                </p:selectOneMenu>
                                <h:inputText id="amount" value="#{ukelonnAdmin.newPayment}" required="true"/>
                                <p:commandButton value="Registrer betaling" update="paymenttype amount :tabs:balanse:balanse :tabs:userinfo:payments:payments" actionListener="#{ukelonnAdmin.registerNewPayment}" />
                            </p:panelGrid>
                        </h:form>
                        <p:accordionPanel id="userinfo" activeIndex="-1">
                            <p:tab title="Siste jobber">
                                <h:form id="jobs">
                                    <p:panelGrid columns="1">
                                        <p:dataTable id="jobs" var="job" value="#{ukelonnAdmin.jobs}" selection="${ukelonnAdmin.jobsSelectedForDelete}" rowKey="${job.id}">
                                            <p:columnGroup type="header">
                                                <p:column headerText=" " />
                                                <p:column headerText="Dato" />
                                                <p:column headerText="Type jobb" />
                                                <p:column headerText="Bel�p" />
                                            </p:columnGroup>
                                            <p:column selectionMode="multiple" style="width:16px;text-align:center"/>
                                            <p:column><h:outputText value="#{job.transactionTime}"/></p:column>
                                            <p:column><h:outputText value="#{job.transactionType.transactionTypeName}"/></p:column>
                                            <p:column><h:outputText value="#{job.transactionAmount}"/></p:column>
                                        </p:dataTable>
                                        <p:commandButton value="Slett valgte jobber" update="jobs :tabs:balanse:balanse" actionListener="#{ukelonnAdmin.deleteSelectedJobs}" />
                                    </p:panelGrid>
                                </h:form>
                            </p:tab>
                            <p:tab title="Siste utbetalinger">
                                <h:form id="payments">
                                    <p:panelGrid columns="1">
                                        <p:dataTable id="payments" var="payment" value="#{ukelonnAdmin.payments}">
                                            <p:columnGroup type="header">
                                                <p:column headerText="Dato" />
                                                <p:column headerText="Type utbetaling" />
                                                <p:column headerText="Bel�p" />
                                            </p:columnGroup>
                                            <p:column><h:outputText value="#{payment.transactionTime}"/></p:column>
                                            <p:column><h:outputText value="#{payment.transactionType.transactionTypeName}"/></p:column>
                                            <p:column><h:outputText value="#{payment.transactionAmount}"/></p:column>
                                        </p:dataTable>
                                    </p:panelGrid>
                                </h:form>
                            </p:tab>
                        </p:accordionPanel>
                    </p:panelGrid>
                </p:tab>
                <p:tab title="Administrere jobbtyper">
                    <p:accordionPanel id="jobtypes">
                        <p:tab title="Lag ny jobbtype">
                            <h:form id="newjobtype">
                                <p:panelGrid columns="2">
                                    <h:outputLabel for="name" value="Navn p� ny jobbtype:" />
                                    <h:inputText id="name" value="#{ukelonnAdmin.newJobTypeName}" required="true"/>
                                    <h:outputLabel for="amount" value="Bel�p for ny jobbtype:" />
                                    <h:inputText id="amount" value="#{ukelonnAdmin.newJobTypeAmount}" required="true"/>
                                    <p:commandButton value="Lag jobbtype" update="name amount :tabs:jobtypes:jobtypesform:editjobtypes" actionListener="#{ukelonnAdmin.registerNewJobType}" />
                                </p:panelGrid>
                            </h:form>
                        </p:tab>
                        <p:tab title="Endre jobbtyper">
                            <h:form id="jobtypesform">
                                <p:dataTable id="editjobtypes" var="jobType" value="#{ukelonnAdmin.jobTypes}" editable="true" editMode="cell" widgetVar="cellJobTypes">
                                    <f:facet name="header">Klikk i celle for � endre verdi</f:facet>

                                    <p:ajax event="cellEdit" listener="#{ukelonnAdmin.onJobTypeCellEdit}" update="" immediate="true" />

                                    <p:column headerText="Navn">
                                        <p:cellEditor>
                                            <f:facet name="output"><h:outputText value="#{jobType.transactionTypeName}" /></f:facet>
                                            <f:facet name="input"><p:inputText value="#{jobType.transactionTypeName}" style="width:96%" label="Navn"/></f:facet>
                                        </p:cellEditor>
                                    </p:column>

                                    <p:column headerText="Bel�p">
                                        <p:cellEditor>
                                            <f:facet name="output"><h:outputText value="#{jobType.transactionAmount}" /></f:facet>
                                            <f:facet name="input"><p:inputText value="#{jobType.transactionAmount}" style="width:96%" label="Navn"/></f:facet>
                                        </p:cellEditor>
                                    </p:column>
                                </p:dataTable>
                            </h:form>
                        </p:tab>
                    </p:accordionPanel>
                </p:tab>
                <p:tab title="Administrere typer av utbetaling">
                    <p:accordionPanel id="paymenttypeadmin">
                        <p:tab title="Lag ny utbetalingstype">
                            <h:form id="newpaymenttype">
                                <p:panelGrid columns="2">
                                    <h:outputLabel for="name" value="Navn p� ny betalingstype:" />
                                    <h:inputText id="name" value="#{ukelonnAdmin.newPaymentTypeName}" required="true"/>
                                    <h:outputLabel for="amount" value="Bel�p for ny betalingstype (kan v�re tomt):" />
                                    <h:inputText id="amount" value="#{ukelonnAdmin.newPaymentTypeAmount}" required="false"/>
                                    <p:commandButton value="Lag betalingstype" update="name amount :tabs:utbetal:paymenttype :tabs:paymenttypeadmin:paymenttypesform" actionListener="#{ukelonnAdmin.registerNewPaymentType}" />
                                </p:panelGrid>
                            </h:form>
                        </p:tab>
                        <p:tab title="Endre utbetalingstyper">
                            <h:form id="paymenttypesform">
                                <p:dataTable id="editpaymenttypes" var="paymentType" value="#{ukelonnAdmin.paymentTypes}" widgetVar="cellPaymentTypes">
                                    <p:column headerText="Navn">
                                        <p:cellEditor>
                                            <f:facet name="output"><h:outputText value="#{paymentType.transactionTypeName}" /></f:facet>
                                            <f:facet name="input"><p:inputText value="#{paymentType.transactionTypeName}" style="width:96%" label="Navn"/></f:facet>
                                        </p:cellEditor>
                                    </p:column>

                                    <p:column headerText="Bel�p">
                                        <p:cellEditor>
                                            <f:facet name="output"><h:outputText value="#{paymentType.transactionAmount}" /></f:facet>
                                            <f:facet name="input"><p:inputText value="#{paymentType.transactionAmount}" style="width:96%" label="Navn"/></f:facet>
                                        </p:cellEditor>
                                    </p:column>
                                </p:dataTable>
                            </h:form>
                        </p:tab>
                    </p:accordionPanel>
                </p:tab>
                <p:tab title="Administrere brukere">
                    <p:accordionPanel id="useradmin">
                        <p:tab title="Legg til ny bruker">
                            <h:form id="newuser">
                                <p:messages id="messages" showDetail="true" autoUpdate="true" />
                                <p:panelGrid columns="2">
                                    <h:outputLabel for="username" value="Brukernavn:" />
                                    <h:inputText id="username" value="#{ukelonnAdmin.newUserUsername}" required="true"/>
                                    <h:outputLabel for="password1" value="Passord:" />
                                    <p:password id="password1" value="#{ukelonnAdmin.newUserPassword1}" label="Passord 1" required="true" feedback="true" inline="true"/>
                                    <h:outputLabel for="password2" value="Gjenta passord:" />
                                    <p:password id="password2" value="#{ukelonnAdmin.newUserPassword2}" label="Passord 2" required="true" feedback="true" inline="true"/>
                                    <h:outputLabel for="email" value="Epostadresse:" />
                                    <h:inputText id="email" value="#{ukelonnAdmin.newUserEmail}" required="true">
                                        <f:validateRegex pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$" />
                                    </h:inputText>
                                    <h:outputLabel for="firstname" value="Fornavn:" />
                                    <h:inputText id="firstname" value="#{ukelonnAdmin.newUserFirstname}" required="true"/>
                                    <h:outputLabel for="lastname" value="Etternavn:" />
                                    <h:inputText id="lastname" value="#{ukelonnAdmin.newUserLastname}" required="true"/>
                                    <p:commandButton value="Lag bruker" update="username password1 password2 email firstname lastname :tabs:useradmin:users:usertable :tabs:selectUser:selectedAccount" actionListener="#{ukelonnAdmin.registerNewUser}" />
                                </p:panelGrid>
                            </h:form>
                        </p:tab>
                        <p:tab title="Bytt passord p� bruker">
                            <h:form id="changeuserpassword">
                                <p:panelGrid columns="2">
                                    <h:outputLabel for="username" value="Velg bruker:" />
                                    <p:selectOneMenu id="selectedUser" value="#{ukelonnAdmin.changePasswordForUser}" converter="userConverter">
                                        <f:selectItem itemLabel="Velg bruker" itemValue="" noSelectionOption="true" />
                                        <f:selectItems value="#{ukelonnAdmin.users}" var="user2" itemValue="#{user2}" itemLabel="#{user2.fullname}"/>
                                        <p:ajax event="change" update="password1 password2"/>
                                    </p:selectOneMenu>
                                    <h:outputLabel for="password1" value="Passord:" />
                                    <p:password id="password1" value="#{ukelonnAdmin.changePasswordForUserPassword1}" label="Passord 1" required="true" feedback="true" inline="true"/>
                                    <h:outputLabel for="password2" value="Gjenta passord:" />
                                    <p:password id="password2" value="#{ukelonnAdmin.changePasswordForUserPassword2}" label="Passord 2" required="true" feedback="true" inline="true"/>
                                    <p:commandButton value="Endre passord" update="selectedUser password1 password2" actionListener="#{ukelonnAdmin.changeUserPassword}" />
                                </p:panelGrid>
                            </h:form>
                        </p:tab>
                        <p:tab title="Endre brukere">
                            <h:form id="users">
                                <p:dataTable id="usertable" var="user" value="#{ukelonnAdmin.users}">
                                    <p:columnGroup type="header">
                                        <p:column headerText="Id" />
                                        <p:column headerText="Brukernavn" />
                                        <p:column headerText="Epost" />
                                        <p:column headerText="Fornavn" />
                                        <p:column headerText="Etternavn" />
                                    </p:columnGroup>
                                    <p:column><h:outputText value="#{user.userId}"/></p:column>
                                    <p:column><h:outputText value="#{user.username}"/></p:column>
                                    <p:column><h:outputText value="#{user.email}"/></p:column>
                                    <p:column><h:outputText value="#{user.firstname}"/></p:column>
                                    <p:column><h:outputText value="#{user.lastname}"/></p:column>
                                </p:dataTable>
                            </h:form>
                        </p:tab>
                    </p:accordionPanel>
                </p:tab>
            </p:accordionPanel>
        </f:view>
    </h:body>
</html>
